package org.jsrml.core.purchaser.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.core.base.entity.Role;
import org.jsrml.core.base.qo.RoleQO;
import org.jsrml.core.base.service.AuthAccountService;
import org.jsrml.core.base.service.RoleService;
import org.jsrml.core.purchaser.command.RemoveWorkerCo;
import org.jsrml.core.base.command.SaveAuthAccountCo;
import org.jsrml.core.purchaser.command.SaveWorkerCo;
import org.jsrml.core.base.entity.AuthAccount;
import org.jsrml.core.purchaser.entity.Positions;
import org.jsrml.core.purchaser.entity.Worker;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.WorkerQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class WorkerService extends BaseDao<Worker, WorkerQO> {

	@Autowired
	private AuthAccountService authAccountService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private PositionsService positionsService;

	public Worker createWorker(SaveWorkerCo command) throws PurchaserException {

		Set<Role> roles = queryRoles(command.getRoleIds());
		
		if (StringUtils.isBlank(command.getPositionsId())) {
			throw new PurchaserException(PurchaserException.NECESSARY_PARAM_NULL, "缺少必要参数职务id");
		}

		Positions positions = positionsService.get(command.getPositionsId());

		if (positions == null) {
			throw new PurchaserException(PurchaserException.POSITIONS_NOT_FOUND, "职务没有找到");
		}

		Worker worker = new Worker();
		worker.create(command, roles, positions.getDepartment().getCompany(), positions.getDepartment(), positions);

		/**
		 * 创建账号
		 */
		if (StringUtils.isBlank(command.getLoginName())) {
			throw new PurchaserException(PurchaserException.NECESSARY_PARAM_NULL, "工作人员登录账号需要填写！");
		}

		SaveAuthAccountCo command2 = new SaveAuthAccountCo();
		command2.setDomainId(worker.getId());
		command2.setClientType("worker");
		command2.setLoginName(command.getLoginName());
		command2.setPassword(command.getPassword());

		AuthAccount account = new AuthAccount();
		account.create(command2);
		authAccountService.save(account);
		worker.getBaseInfo().setAuthAccountId(account.getId());
		save(worker);
		return worker;
	}

	public Worker modifyWorker(SaveWorkerCo command) throws PurchaserException {

		Set<Role> roles = queryRoles(command.getRoleIds());

		if (StringUtils.isBlank(command.getPositionsId())) {
			throw new PurchaserException(PurchaserException.NECESSARY_PARAM_NULL, "缺少必要参数职务id");
		}

		Positions positions = positionsService.get(command.getPositionsId());

		if (positions == null) {
			throw new PurchaserException(PurchaserException.POSITIONS_NOT_FOUND, "职务没有找到");
		}

		Worker worker = get(command.getWorkerId());
		worker.modify(command, roles, positions.getDepartment().getCompany(), positions.getDepartment(), positions);

		if (StringUtils.isBlank(command.getLoginName())) {
			throw new PurchaserException(PurchaserException.POSITIONS_NOT_FOUND, "工作人员登录账号需要填写！");
		}

		if (StringUtils.isBlank(command.getPassword())) {
			throw new PurchaserException(PurchaserException.POSITIONS_NOT_FOUND, "工作人员登录密码需要填写！");
		}

		AuthAccount account = authAccountService.get(worker.getBaseInfo().getAuthAccountId());

		SaveAuthAccountCo command2 = new SaveAuthAccountCo();
		command2.setAuthAccountId(account.getId());
		command2.setLoginName(account.getLoginName());
		command2.setPassword(command.getPassword());
		account.modify(command2);
		authAccountService.save(account);

		return worker;
	}

	public void removeWorker(RemoveWorkerCo command) throws PurchaserException {

		if (StringUtils.isBlank(command.getWorkerId())) {
			throw new PurchaserException(PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}

		Worker worker = get(command.getWorkerId());

		if (worker == null) {
			throw new PurchaserException(PurchaserException.WORKER_NOT_FOUND, "主机方没有找到!");
		}

		worker.remove();
	}

	@Transactional(rollbackFor = PurchaserException.class)
	public void plRemoveWorker(RemoveWorkerCo command) throws PurchaserException {
		if (command.getWorkerIds() == null || command.getWorkerIds().size() == 0) {
			throw new PurchaserException(PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}

		for (String workerId : command.getWorkerIds()) {
			Worker worker = get(workerId);
			if (worker == null) {
				throw new PurchaserException(PurchaserException.WORKER_NOT_FOUND, "主机方没有找到!");
			}

			worker.getStatus().setRemove(true);

			save(worker);
		}

	}

	private Set<Role> queryRoles(List<String> roleIds) {
		RoleQO qo = new RoleQO();
		qo.setIds(roleIds);
		List<Role> roleList = roleService.queryList(qo);
		Set<Role> roles = new HashSet<Role>();
		if (roleList == null) {
			return roles;
		}
		for (Role role : roleList) {
			roles.add(role);
		}
		return roles;
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, WorkerQO qo) {
		return criteria;
	}

	@Override
	protected Class<Worker> getEntityClass() {
		return Worker.class;
	}

}
